package com.shanghaionstar.dealersorder.presenter;

import android.content.Context;

import com.onstar.cn.framework.mvp.base.IMVPScheduler;
import com.onstar.cn.framework.mvp.base.core.BasePresenter;
import com.shanghaionstar.dealersorder.bean.DealerOrderRecord;
import com.shanghaionstar.dealersorder.model.OrderRecordModel;
import com.shanghaionstar.dealersorder.view.OrderRecordView;

import rx.functions.Action1;

/**
 * Created by Yonglang Hong on 2016/8/11.
 */
public class OrderRecordPresenter  extends BasePresenter {

    OrderRecordView  recordView;

    OrderRecordModel recordModel;

    public void setOrderView(OrderRecordView recordView) {
        this.recordView = recordView;
    }

    public OrderRecordPresenter(IMVPScheduler mvpScheduler, Context context) {
        super(mvpScheduler);
        recordModel = new OrderRecordModel(context);
    }

    public void loadRecords() {
        subscription = recordModel.loadRecords()
                .subscribeOn(mvpScheduler.backgroundThread())
                .observeOn(mvpScheduler.mainThread())
                .subscribe(new Action1<DealerOrderRecord>() {
                    @Override
                    public void call(DealerOrderRecord record) {
                        if (record == null || record.getOrders() == null || record.getOrders().isEmpty())
                            recordView.showEmpty();
                        else
                            recordView.showRecordList(record);
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        //里面抛出的Error信息
                        recordView.showErr(throwable);
                    }
                });
    }
}
